package dao

import javax.inject.{Inject, Singleton}
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 2018/7/17
 */
@Singleton
class QueryFsJunctionDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  val table = QueryFsJunction

  def insertAll(rows: List[QueryFsJunctionRow]) = db.run(QueryFsJunction ++= rows).map(_ => ())

  def select(junctionId: String) = db.run(QueryFsJunction.
    filter(_.junctionId === junctionId).result.head)

  def selectAll(geneSymbol: String) = db.run(QueryFsJunction.
    filter(x => x.geneSymbol1 === geneSymbol || x.geneSymbol2 === geneSymbol).result)

  def selectAll(junctionIds: Seq[String]) = db.run(table.
    filter(x => x.junctionId.inSetBind(junctionIds)).result.map(_.toList))

  def selectAllGeneSymbol = db.run(QueryFsJunction.
    map(x => (x.geneSymbol1, x.geneSymbol2)).distinct.result.map(_.toList))

  def deleteAll = db.run(QueryFsJunction.delete).map(_ => ())


}
